//------------------------------------------------------------------------------
// X2TK Graphic Toolkit
// Copyright (C) 2006-2008 Janusz Kowalski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

// !! This shouldn't be in x2tk namespace.

#ifndef X2TK_GRAPHICS_H
#define X2TK_GRAPHICS_H

#include "SDL.h"
#include "imgSDL.h"

class GraphicContext {
  private:
    // Clipping rectangle. All the drawing operations will be performed relative
    // to the top-left corner of this rectangle and inside it.
    SDL_Rect cRect_;
    SDL_Surface *dstSurf_;
    
    // Origin coordinates.
    int ox_, oy_;
    
    // Current color.
    Uint32 color_;
    Uint8 r_,g_,b_;
    Uint8 alpha_;
    
    // Is clipping enabled?
    bool clipToRectangle_;
    
    void clipRectangle(SDL_Rect& victim, int x, int y, int w, int h) const;
    
  private:
    // This works only for SDL drawing mode.
    void blitSurface(SDL_Surface *src, int x, int y) const;
    // This works only for SDL drawing mode.
    void blitSurface(SDL_Surface *src, SDL_Rect *srcRect, int x, int y) const;
  public:
    // Constructor; dstSurf - destination surface.
    GraphicContext(SDL_Surface *dstSurf);
    
    // Destructor.
    ~GraphicContext();
    
    // Should be called before using any direct OpenGL drawing functions.
    void startOpenGL();
    void endOpenGL();

    void drawCircle(float x0, float y0, int r, int l=4);

    // angle is in radians
    void drawTexturedCircle(Image &image, float x0, float y0, float r, float angle=0, int l=1);

    int getW() const {return cRect_.w;}
    int getH() const {return cRect_.h;}
  
    // Limit the current context (not const).
    void limit(int x_rel, int y_rel, int w, int h);
    
    void blitImage(Image &image, int x, int y);

	void drawImage(Image &image, float x, float y);
    
    // Blit image, with corner at the given position, rotated about this corner.
    void TMPblitImage(Image &image, int x, int y, float angle);
    
    // Blit image resizing it to the given dimensions.
    void blitImage(Image &image, int x, int y, int w, int h);
    
    void blitImage(Image &image, SDL_Rect *srcRect, int x, int y) const;
    
    // Fill whole rectangle with tex.
    void textureFill(SDL_Surface *tex) const;
    
    // Fill whole rectangle with color.
    void colorFill(int r, int g, int b) const;
    
    // Fill the given rectangle with color.
    void colorFill(int r, int g, int b, int x0, int y0, int w, int h) const;
    
    // Fill the given rectangle with texture.
    void textureFill(SDL_Surface *tex, int x0, int y0, int w, int h) const;
    
    // Textured polygon.
    void texdPolygon(Sint16 *vx, Sint16 *vy, int n, SDL_Surface *tex, int texDx,int texDy) const;
    
    void filledPolygon(Sint16 *vx, Sint16 *vy, int n) const;
    
    // Horizontal line
    void hLine(int x, int y, int w) const;
    
    void rectangle(int x0, int y0, int w, int h) const;
    void filledRectangle(int x1, int y1, int w, int h) const;
    
    // Line
    void line(int x1, int y1, int x2, int y2);
    
    // Set drawing color. Format is 0xRRGGBBAA.
    void setColor(Uint32 color);
    void setColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a);
    
    // Set clipping.
    void setClipping(bool v);
};

#endif
